<?php
/**
 * Created by PhpStorm.
 * User: 54679  投票专用
 * Date: 2020/8/3
 * Time: 15:54
 */

//     function getOpenId($length =28){
//        //id=2733&company_id=25&activity_id=4&open_id=oJkC8whSXVxnerXD7Y41EN4EQFhA
//        // open_id =  oJkC8whSXVxnerXD7Y41EN4EQFhA  28位
//                   // oJkC8wmLq0nYPKa3OOdSaFD_Hk1A 大哥的
//        $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
//        $open_id = '';
//
//        for ($i=0;$i<=$length;$i++){
//            $open_id .=  substr($str, mt_rand(0, strlen($str)-1), 1);
//        }
//
//        return $open_id;
//    }

     function goAddPiao($length =22){
        // 拿到上面随机的open_id 然后访问3次接口以后重新获取 open_ID
        static $for_num = 0;

         $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
         $open_id = '';

         for ($i=0;$i<=$length;$i++){
             $open_id .= substr($str, mt_rand(0, strlen($str)-1), 1);
         }
         $open_id = 'oJkC8'.$open_id;
        if($for_num == 2){
            $open_id = $this->getOpenId();
        }

        $go_url = "http://hxvote.9lou.net/api/vote";

        $sl_data=array(
            'id'=>2733,
            'company_id'=>25,
            'activity_id'=> 4,
            'open_id'=>$open_id
        );
        for ($i=0;$i<=10;$i++){
            sleep(0.5);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $go_url);//要访问的地址
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);//执行结果是否被返回，0是返回，1是不返回
            curl_setopt($ch, CURLOPT_POST, 1);// 发送一个常规的POST请求
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($sl_data));// 填写参数

            $is_ok =  curl_exec($ch);//执行并获取数据
            curl_close($ch);
            $is_ok = json_decode($is_ok,true);

            var_dump($open_id.'投票成功'.$i.'次');

            if($i%2==0){  // 如果是偶数 就让他重新获取
                var_dump('重新获取了'.$i.'次'.$open_id);
                $for_num = 0;
            }else{
                $for_num++;
            }
        }
    }
      goAddPiao();

